//
//  ViewController.swift
//  chenChat
//
//  Created by chen on 16/2/17.
//  Copyright © 2016年 chen. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet var startButton: UIButton!
    @IBOutlet var pageControl: UIPageControl!
    var scrollView: UIScrollView!
    let numOfPage = 3
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let frame = self.view.bounds
        scrollView = UIScrollView(frame: frame)
        scrollView.pagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.scrollsToTop = false
        scrollView.bounces = false
        scrollView.contentOffset = CGPointZero
        scrollView.contentSize = CGSize(width: frame.size.width * CGFloat(numOfPage), height: frame.size.height)
        scrollView.delegate = self
        for index in 0 ..< numOfPage {
            print("frame.size.width:\(frame.size.width),frame.width:\(frame.width)")
            let imageView = UIImageView(image: UIImage(named: "famliy\(index + 1)"))
            imageView.frame = CGRect(x: frame.size.width * CGFloat(index), y: 0, width: frame.size.width, height: frame.size.height)
            scrollView.addSubview(imageView)
        }
        self.view.insertSubview(scrollView, atIndex: 0)
        
        startButton.layer.cornerRadius = 15.0
        startButton.enabled = false
        startButton.alpha = 0.0
        startButton.addTarget(self, action: "clickButton", forControlEvents: .TouchUpInside)

    }
    func clickButton(){
       (UIApplication.sharedApplication().delegate as! AppDelegate).start()
        self.dismissViewControllerAnimated(true) { () -> Void in
        }
    }
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
}

extension ViewController: UIScrollViewDelegate{
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let offset = scrollView.contentOffset
        pageControl.numberOfPages = numOfPage
        pageControl.currentPage = Int(offset.x / view.bounds.width)
        if pageControl.currentPage == numOfPage - 1{
            UIView.animateWithDuration(0.3, animations: { () -> Void in
                self.startButton.enabled = true
                self.startButton.alpha = 1.0
            })
        }else{
            UIView.animateWithDuration(0.3, animations: { () -> Void in
                self.startButton.enabled = false
                self.startButton.alpha = 0.0
            })
        }
    }
}
